package com.ppc.controller.employer;

import com.ppc.constant.JwtClaimsConstant;
import com.ppc.context.BaseContext;
import com.ppc.dto.EmployerChangeInfoDTO;
import com.ppc.dto.EmployerLoginDTO;
import com.ppc.dto.UserLoginVerifyCodeDTO;

import com.ppc.properties.JwtProperties;
import com.ppc.result.Result;
import com.ppc.service.EmployerService;
import com.ppc.utils.JwtUtil;
import com.ppc.vo.EmployerDataVO;
import com.ppc.vo.EmployerLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 雇主账号相关接口
 *
 * @author LENOVO
 * @date 2023/12/20
 */
@RestController
@RequestMapping("/employer")
@Slf4j
@Api(tags = "雇主信息接口")
public class EmployerController {

  @Autowired
  private JwtProperties jwtProperties;

  @Autowired
  private EmployerService employerService;
  @Autowired
  private RedisTemplate<String, String> redisTemplate;

  /**
   * @return {@link Result }
   * @description
   */
  @PostMapping("/unregister")
  @ApiOperation(value = "雇主注销")
  public Result unregister() {
    employerService.upregister();
    return Result.success();
  }


  /**
   * @param employeeLoginDTO
   * @return {@link Result }<{@link EmployerLoginVO }>
   * @description
   */

  @PostMapping("/login")
  @ApiOperation(value = "雇主登录")
  public Result<EmployerLoginVO> login(@RequestBody EmployerLoginDTO employeeLoginDTO) {
    log.info("雇主登录：{}", employeeLoginDTO);

    EmployerLoginVO employer = employerService.login(employeeLoginDTO);

    //登录成功后，生成jwt令牌
    Map<String, Object> claims = new HashMap<>();
    claims.put(JwtClaimsConstant.EMPLOYER_ID, employer.getUserId());
    String token = JwtUtil.createJWT(
        jwtProperties.getEmployerSecretKey(),
        jwtProperties.getEmployerTtl(),
        claims);

    employer.setToken(token);

    log.info("查询到雇主，{}", employer);
    return Result.success(employer);


  }

  @PostMapping("/login_verify")
  @ApiOperation(value = "雇主验证码登录")
  public Result<EmployerLoginVO> loginVerfiyCode(@RequestBody UserLoginVerifyCodeDTO userLoginVerifyCodeDTO) {
    log.info("雇主验证码登录：{}", userLoginVerifyCodeDTO);

    if(userLoginVerifyCodeDTO.getUserVerfiyCode()==null || userLoginVerifyCodeDTO.getUserVerfiyCode()==""){
      return Result.error("验证码为空");
    }
    else if (userLoginVerifyCodeDTO.getUserEmail()!=null && userLoginVerifyCodeDTO.getUserEmail()!=""){
      String verifycode = (String) redisTemplate.opsForValue().get(userLoginVerifyCodeDTO.getUserEmail());
      if(!verifycode.equals(userLoginVerifyCodeDTO.getUserVerfiyCode())){
        return Result.error("验证码错误");
      }
    }else if(userLoginVerifyCodeDTO.getUserPhone()!=null && userLoginVerifyCodeDTO.getUserPhone()!=""){
      String verifycode = (String) redisTemplate.opsForValue().get(userLoginVerifyCodeDTO.getUserPhone());
      if(!verifycode.equals(userLoginVerifyCodeDTO.getUserVerfiyCode())){
        return Result.error("验证码错误");
      }
    }else {
      return Result.error("错误输入");
    }

    EmployerLoginVO employer = employerService.loginVerfiy(userLoginVerifyCodeDTO);

    //登录成功后，生成jwt令牌
    Map<String, Object> claims = new HashMap<>();
    claims.put(JwtClaimsConstant.EMPLOYER_ID, employer.getUserId());
    String token = JwtUtil.createJWT(
        jwtProperties.getEmployerSecretKey(),
        jwtProperties.getEmployerTtl(),
        claims);

    employer.setToken(token);

    log.info("查询到雇主，{}", employer);
    return Result.success(employer);


  }

  /**
   * 雇主退出
   *
   * @return {@link Result}<{@link String}
   */
  @PostMapping("/logout")
  @ApiOperation("雇主退出")
  public Result<String> logout() {
    log.info("雇主退出");
    return Result.success();
  }


  /**
   * @return {@link Result }<{@link EmployerDataVO }>
   * @description
   */
  @PostMapping("/info")
  @ApiOperation(value = "查看我的资料")
  public Result<EmployerDataVO> employerData() {
    log.info("雇主查看“我的资料”");
    EmployerDataVO employerDataVO = employerService.checkData();

    return Result.success(employerDataVO);
  }

  /**
   * 雇主修改个人信息
   *
   * @param employerChangeInfoDTO
   * @return {@link Result}
   */
  @PostMapping("/info/change")
  @ApiOperation("雇主修改个人信息")
  public Result changeInformation(@RequestBody EmployerChangeInfoDTO employerChangeInfoDTO) {
    log.info("修改雇主个人信息:{}", employerChangeInfoDTO);
    employerService.changeInformation(employerChangeInfoDTO);
    return Result.success();
  }

  /**
   * 雇主充值
   * @param amount
   * @return
   */
  @PostMapping("/recharge")
  @ApiOperation("雇主充值")
  public Result employerRecharge(@RequestParam(required = false) Float amount) {
    log.info("雇主充值");
    employerService.employerRecharge(amount);
    return Result.success();
  }

  /**
   * @return {@link Result }<{@link Integer }>
   * @description
   */
  @GetMapping("/credit")
  @ApiOperation(value = "查看我的信誉")
  public Result<Integer> employerCredit() {

    log.info("雇主查看我的信誉");
    int employerCredit = employerService.checkCredit();
    return Result.success(employerCredit);
  }

  /**
   * 查看我的钱包余额
   *
   * @return {@link Result}<{@link Integer}>
   */
  @GetMapping("/wallet")
  @ApiOperation(value = "查看我的钱包余额")
  public Result<Float> employerWallet() {
    log.info("雇主查看我的钱包余额");
    Float employerCount = employerService.checkWallet();
    return Result.success(employerCount);
  }

  /**
   * @return {@link Result }<{@link List }>
   * @description
   */
  @GetMapping("/credit/recording")
  @ApiOperation(value = "查看我的信誉记录")
  public Result<List> employerCreditRecording() {
    Long userId = BaseContext.getCurrentId();
    log.info("雇主查看我的信誉：{}", userId);
    List<String> recordings = employerService.checkCreditRecording(userId);
    return Result.success(recordings);
  }

  /**
   * @return {@link Result }<{@link List }>
   * @description
   */
  @GetMapping("/count/recording")
  @ApiOperation(value = "查看我的钱包记录")
  public Result<List> employerCountRecording() {
    Long userId = BaseContext.getCurrentId();
    log.info("雇主查看我的钱包记录：{}", userId);
    List<String> recordings = employerService.checkCountRecording(userId);
    return Result.success(recordings);
  }

  /**
   * @param taskId
   * @param workerRating
   * @return {@link Result }
   * @description
   */
  @PostMapping("/comment")
  @ApiOperation(value = "雇主评价工人")
  public Result empComment(Long taskId, int workerRating) {
    log.info("雇主评价工人：{}", taskId);
    employerService.commentWorker(taskId, workerRating);
    employerService.updateWorkerCredit(taskId, workerRating);
    return Result.success();
  }

  /**
   * @param taskId
   * @param workerRating
   * @return {@link Result }
   * @description
   */
  @PostMapping("/credit")
  @ApiOperation(value = "工人信誉变化记录")
  public Result createCreditRecording(Long taskId, int workerRating) {
    log.info("工人信誉变化记录：{}", workerRating);
    employerService.createCreditRecording(taskId, workerRating);
    return Result.success();
  }

  /**
   * @param file
   * @return {@link Result }<{@link String }>
   * @description
   */
  @PostMapping("/upload_img")
  @ApiOperation("上传头像")
  public Result<Long> uploadImg(@RequestPart("file") MultipartFile file) {
    log.info("头像上传{}", file);
    Long fileId = employerService.uploadImg(file);

    return Result.success(fileId);

  }

  /**
   * @param fileId
   * @return {@link Result }<{@link String }>
   * @description
   */
  @PostMapping("/down_file")
  @ApiOperation("下载文件")
  public Result<String> downFile(Long fileId) {
    log.info("文件下载{}", fileId);

    String filePath = employerService.downFile(fileId);

    return Result.success(filePath);

  }

}
